package org.wmine.algo;

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Vector;


import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class clusteringNext extends JFrame
{
	private static final int CENTER = 0;
	private static final int RIGHT = 0;
	int i;String classLbl;
	String []seeds;
	int [] checkCnt;
	int [] selected;int []selectedCol;
	String [][]clusters;
	String[]attribs1;
	int colknt1,cntRow1;String Goalitem;
	String[][]data2;
	TextField [] tf;//=new TextField();
	int tfText1;
	String SeedsCalc;
	int[] selected1;
	String [][]recBelongs2;
	//public clusteringNext(final String[]attribs,final int colknt,final int cntRow,final String [][] data)
	public clusteringNext(final String[]attribs,final int colknt,final int cntRow,final String[][]dataValues,String classLbl1,String tfText,int[]selected)
	{
		tfText1=Integer.parseInt(tfText);//no.of clusters
		attribs1=attribs;//column names array
		colknt1=colknt;//no of columns
		cntRow1=cntRow;// no of rows
		data2=dataValues;//2d array of data
		SeedsCalc=classLbl1;// manually or automatically
		selected1=selected;//columns under consideration are made 1
		
		tf=new TextField[tfText1];
		
		seeds=new String[tfText1];
		
		System.out.println(colknt1+">>"+tfText1+">>"+seeds.length);
		final Container c=this.getContentPane();
		this.setTitle("Seed Points");
		System.out.println(colknt1+">>>>>>>>>>11111");
		JPanel mainPanel=new JPanel();
		mainPanel.setLayout(new GridLayout(Integer.parseInt(tfText)+3,2));
		mainPanel.add(new JLabel("Seed Points:"));
		mainPanel.add(new JLabel(""));
	
		for(int j = 0;j<Integer.parseInt(tfText);j++)
		{
		
			tf[j]=new TextField();
			mainPanel.add(new JLabel("Cluster Number:"+(j+1)));
			mainPanel.add(tf[j]);
		}
		
		mainPanel.add(new JLabel(""));
		mainPanel.add(new JLabel(""));
		
		JButton but1=new JButton("OK");
		JButton but2=new JButton("Cancel");
	
		mainPanel.add(but1);
		mainPanel.add(but2);
		System.out.print("before action listener");
		but1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent ae)
		{
			dispose();
			
			
			for(int s=0;s<tf.length;s++)
			{
				seeds[s]=tf[s].getText();
				System.out.println(seeds[s]+"???????");				
			}
			System.out.println("No Probs");
			JOptionPane.showMessageDialog(null,"!!!!");
			kmeans();
			JOptionPane.showMessageDialog(null,"Great!!!!!");
			
		}});		
		System.out.print("after action listener");
		c.add(mainPanel);
		this.setBounds(300,200,300,(36*(tfText1+1)));
	
	}
	public void kmeans()
	{
		System.out.println("in k");
		/*tfText1=Integer.parseInt(tfText);//no.of clusters
		attribs1=attribs;//column names array
		colknt1=colknt;//no of columns
		cntRow1=cntRow;// no of rows
		data2=dataValues;//2d array of data
		SeedsCalc=classLbl1;// manually or automatically
		selected1=selected;//columns under consideration are made 1		*/
		int cnt1=0;//count of no of attribs under consideration
		System.out.println("b4 kmean");
		for(int i=0;i<selected1.length;i++)
			if(selected1[i]==1)cnt1++;
		selectedCol=new int[cnt1];//stores attrib column numbers
		int ptr=0;
		for(int i=0;i<selected1.length;i++)
			if(selected1[i]==1) selectedCol[ptr++]=i;
		System.out.println("Inside kmean"+tfText1+","+cnt1);
		clusters=new String[tfText1][cnt1];// it can be double
		for(int cl=0;cl<tfText1;cl++)
		{
			for(int slct=0;slct<cnt1;slct++)
			{
				clusters[cl][slct]=data2[Integer.parseInt(seeds[cl])][selectedCol[slct]];
				System.out.println(">>>>>>>"+clusters[cl][slct]);
			}			
		}
		System.out.println("come out of it!");
		
		recBelongs2=new String[cntRow1][2];//o=>distance, 1=>cluster no
		int flagChanged=0;
		int firstTime=0;
		do
		{
			for(int r=0;r<cntRow1;r++)
			{
				String [] retVal=dist(r,0);//distance of rth record from 0th cluster center
				System.out.println("before finding distance1");
				double distance=Double.valueOf(retVal[0]);
				int clusterNo=Integer.parseInt(retVal[1]);
				System.out.println("before finding distance2");
				double temp0=0.0;
				int temp1=0;
				if(firstTime!=0)
				{
				temp0=Double.valueOf(recBelongs2[r][0]);
				temp1=Integer.parseInt(recBelongs2[r][1]);
				}
				System.out.println("before finding distance3");
				for(int k=0;k<tfText1;k++)
				{
					retVal=dist(r,k);
					if(distance>Double.valueOf(retVal[0]))
					{
						distance=Double.valueOf(retVal[0]);
						clusterNo=Integer.parseInt(retVal[1]);
					}					
				}
				recBelongs2[r][0]=distance+"";
				recBelongs2[r][1]=clusterNo+"";
				if(firstTime==0) flagChanged=0;
				else
				{
					System.out.println("Inside else"+temp0+","+temp1);
					if(temp1!=Integer.parseInt(recBelongs2[r][1]))
					{
						flagChanged=1;// clusters got changed
					}
				}
			}			
			if(flagChanged==0)
			{
				System.out.println("OK..its working here");
				
				if(firstTime==1)//converged!!!
					{
						showClusters();
						break;
					
					}
				else
					findCenters();
			}
			else				//not a first time , but clusters got changed
				{
					findCenters();
				}
			firstTime=1;
		}while(flagChanged==0);		
	}
	public void showClusters()
	{
		for(int cn=0;cn<tfText1;cn++)// Value at each index
		{
			for(int rec=0;rec<cntRow1;rec++)
			{
				if(Integer.parseInt(recBelongs2[rec][1])==cn)
				{
					System.out.println("Cluster "+cn+" : Record "+rec);
				}				
			}
			System.out.println("***********************************");
		}
		
		
	}
	public void findCenters()
	{
		System.out.println("OK..its working");
		// find the mean of those belonging to corresponding cluster, reset the clusters[][] values
		for(int cn=0;cn<tfText1;cn++)// Value at each index
		{
			double []tots=new double[selectedCol.length];
			for(int col=0;col<selectedCol.length;col++)//initialize earlier values if needed
			tots[col]=0.0;
			//double sum=0;
			
			int itemNo=0;
			
			for(int rec=0;rec<cntRow1;rec++)
			{
				if(Integer.parseInt(recBelongs2[rec][1])==cn)
				{
					//here mean of those n selected cols shud b taken 
					//sum+=Double.valueOf(recBelongs2[rec][0]);
					itemNo++;
					for(int z=0;z<selectedCol.length;z++)
					{
						tots[z]+=Double.valueOf(data2[rec][selectedCol[z]]);						
					}
				}				
			}
			for(int z=0;z<selectedCol.length;z++)
			{
				clusters[cn][z]=(double)(tots[z]/(double)itemNo)+"";
			}
		}
		
		
	}
public String [] dist(int recNo,int clustNo)
{
	String [] ret=new String[2];
	double sum=0.00;
	for(int d=0;d<selectedCol.length;d++)
	{
		sum+=Math.pow(Double.valueOf(data2[recNo][selectedCol[d]])-Double.valueOf(clusters[clustNo][d]),2);
	}
	double val=Math.sqrt(sum);
	ret[0]=""+val;
	ret[1]=clustNo+"";
	System.out.println(">>"+sum+"?"+ret[0]+","+ret[1]+"("+recNo+","+clustNo+")");
	return ret;
}
}

